﻿using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using Red.Controls.Utils;
using Red.Controls.ViewModels;
using Red.EngineData.Protocol.Interop;

namespace Red.Controls
{
    /// <summary>
    /// Control shows debug information about interop execution process
    /// </summary>
    public partial class InteropConsole : UserControl
    {
        #region Constructor

        /// <summary>
        /// Default constructor
        /// </summary>
        public InteropConsole()
        {
            InitializeComponent();

            this.Loaded += OnControlLoaded;
        }

        #endregion

        #region Private Methods

        private void OnControlLoaded( object sender, RoutedEventArgs e )
        {
            CollectionView view = (CollectionView)CollectionViewSource.GetDefaultView( InteropDebugEntries.ItemsSource );
            view.Filter = UserFilter;

            OwnerFilter.SelectedIndex = 0;
        }

        /// <summary>
        /// Filters data in grid
        /// </summary>
        /// <param name="item">Currently filtering item</param>
        /// <returns>Is item visible</returns>
        private bool UserFilter( object item )
        {
            InteropDebugEntry entry = (InteropDebugEntry)item;

            if ( entry.Type == InteropDebugEntryType.Error && ErrorsFilter.IsChecked == false )
                return false;
            if ( entry.Type == InteropDebugEntryType.Warning && WarningsFilter.IsChecked == false )
                return false;
            if ( entry.Type == InteropDebugEntryType.DebugMessage && MessagesFilter.IsChecked == false )
                return false;

            string ownerFilter = (string)OwnerFilter.SelectedValue;
            if ( ownerFilter != "All" )
            {
                if ( entry.OwnerElement != ownerFilter )
                    return false;
            }

            return true;
        }

        private void RefreshList()
        {
            if ( InteropDebugEntries != null )
            {
                CollectionView view = (CollectionView)CollectionViewSource.GetDefaultView( InteropDebugEntries.ItemsSource );
                if ( view != null )
                    view.Refresh();
            }
        }

        private void TypeFilter_Click( object sender, RoutedEventArgs e )
        {
            RefreshList();
        }

        private void OwnerFilter_SelectionChanged( object sender, SelectionChangedEventArgs e )
        {
            RefreshList();
        }

        private void InteropDebugEntries_MouseDoubleClick( object sender, System.Windows.Input.MouseButtonEventArgs e )
        {
            DataGrid dataGrid = sender as DataGrid;
            InteropDebugEntry entry = dataGrid.SelectedItem as InteropDebugEntry;

            if ( entry != null && string.IsNullOrEmpty( entry.File ) == false && entry.Line != -1 )
            {
                VisualStudioHelper.OpenLocalization( entry.File, entry.Line );
            }
        }

        private void ClearLogs_Click( object sender, RoutedEventArgs e )
        {
            var data = (InteropDebugConsoleViewModel)DataContext;
            data.ClearLogs();
        }

        #endregion
    }
}
